function doBid()
{

}

function payDeposit(callback)
{
  const app = getApp();
  app.httpPost('/index.php?r=v1/deposit/index', {
    success: function (res) {
      console.log(res);
      if (res.code != 200) {
        wx.showToast({
          title: res.data,
        })
        return;
      }
      var data = res.data;
      console.log(data);
      wx.requestPayment({
        timeStamp: data.timeStamp + '',
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.paySign,
        'success': function (res) {
          console.log(res);
          if (res.errMsg == 'requestPayment:ok')
          {
            wx.showToast({
              title: '缴纳成功',
            })
            if(callback.success)
            {
              setTimeout(function(){
                callback.success()
              },3000);
            }
          }
        },
        'fail': function (res) {
          console.log(res);
        }
      });
    }
  }, {});
}

function payOrder(page, order_id)
{
  const app = getApp();
  app.httpPost('/index.php?r=v1/payment/index', {
    success: function (res) {
      console.log(res);
      if (res.code != 200) {
        wx.showToast({
          title: res.data,
        })
        return;
      }
      var data = res.data;
      console.log(data);
      wx.requestPayment({
        timeStamp: data.timeStamp + '',
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.paySign,
        'success': function (res) {
          console.log(res);
          if (res.errMsg == 'requestPayment:ok') {
            wx.showToast({
              title: '支付成功',
            })
          }
        },
        'fail': function (res) {
          console.log(res);
        }
      });
    }
  }, {order_id:order_id});
}

function onLoveClicked(page, e) {
  var productId = e.currentTarget.dataset.id;
  var item = page.getProductItem(productId);
  var self = page;

  if (item.is_favorite == true) {
    app.httpDelete('/index.php?r=v1/favorite/index&id=' + productId, {
      success: function (res) {
        console.log(res);
        item.is_favorite = res.data.is_favorite;
        self.updateProducts();
        wx.showToast({
          title: '已取消',
          icon: 'none'
        });
      },
    });
  }
  else {
    app.httpPost('/index.php?r=v1/favorite/index', {
      success: function (res) {
        console.log(res);
        item.is_favorite = res.data.is_favorite;
        self.updateProducts();
        wx.showToast({
          title: '已关注',
        });
      },
    },
      { product_id: productId }
    );
  }
}

function onBidClicked(page, e) {
  var productId = e.currentTarget.dataset.id;
  var item = page.getProductItem(productId);
  var self = page;

  if (item.next_bid >= item.price) {
    self.onBuyClicked(e);
    return;
  }

  if (item.can_bid) {
    wx.showModal({
      title: '确认要出价吗？',
      confirmText: '出价',
      content: item.next_bid + '',
      success: function (res) {
        if (res.confirm) {
          console.log('bid');
          app.httpPost('/index.php?r=v1/bid/index', {
            success: function (res) {
              console.log(res);
              if (res.code == 200) {
                item.bid_total = res.data.bid_total;
                item.next_bid = res.data.next_bid;
                self.updateProducts();
              }
              else if (res.code == 100800) {
                wx.showModal({
                  title: '出价错误',
                  content: '你需要缴纳保证金后才能参与拍卖,请先缴纳保证金',
                  confirmText: '缴纳保证金',
                  success: function (res) {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '/pages/my/paydeposit',
                      });
                    }
                  }
                })
              }
              else {
                wx.showToast({
                  title: res.msg,
                })
              }
            }
          }, {
              product_id: item.id,
              price: item.next_bid
            });
        }
      }
    })
  }
  else {
    wx.showToast({
      title: '尚未开始',
      icon: 'none'
    })
  }
}

function onBuyClicked(e) {
  var productId = e.currentTarget.dataset.id;
  var item = page.getProductItem(productId);
  var self = page;

  if (item.can_bid) {
    wx.showModal({
      title: '确认要出价吗？',
      confirmText: '出价',
      content: item.price + '',
      success: function (res) {
        if (res.confirm) {
          console.log('bid');
          app.httpPost('/index.php?r=v1/bid/buy', {
            success: function (res) {
              console.log(res);
              if (res.code == 200) {
                item.bid_total = res.data.bid_total;
                item.next_bid = res.data.next_bid;
                self.updateProducts();
                if (res.data.order_id) {
                  var url = "/pages/my/confirm?id=" + res.data.order_id;
                  wx.navigateTo({
                    url: url,
                  })
                }
              }
              else if (res.code == 100800) {
                wx.showModal({
                  title: '出价错误',
                  content: '你需要缴纳保证金后才能参与拍卖,请先缴纳保证金',
                  confirmText: '缴纳保证金',
                  success: function (res) {
                    if (res.confirm) {
                      action.payDeposit(self);
                    }
                  }
                })
              }
              else {
                wx.showToast({
                  title: res.msg,
                })
              }
            }
          }, {
              product_id: item.id,
              price: item.next_bid
            });
        }
      }
    })
  }
  else {
    wx.showToast({
      title: '尚未开始',
      icon: 'none'
    })
  }

}


function test()
{
  console.log('test');
}

module.exports = {
  doBid,
  payDeposit,
  payOrder,
  test
}